package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.vo.RecommendUserVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.TanHuaService;
import com.itheima.service.mongo.UserLikeCountService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class TanHuaManager {

    @DubboReference
    private TanHuaService tanHuaService;
    @DubboReference
    private UserInfoService userInfoService;
    @DubboReference
    private UserLikeCountService userLikeCountService;
    @DubboReference
    private FriendService friendService;
    public ResponseEntity showCards() {
        Long toUserId = UserHolder.getUserId();
        List<RecommendUser> recommendUserList = tanHuaService.findRecommendUsers(toUserId);

        List<RecommendUserVo> recommendUserVoList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(recommendUserList)){
            for (RecommendUser recommendUser : recommendUserList) {
                //左滑:score-100 右滑:score+100:保证该用户不会再次出现在探花列表中
                //没有左滑或右滑,正常添加进vo展示
                //if(score>0&&score<100){ 改成删除了
                //获取推荐对象的id,通过推荐对象的id,查询推荐对象的个人信息(userInfo)
                UserInfo userInfo = userInfoService.findByUserId(recommendUser.getUserId());

                RecommendUserVo vo = new RecommendUserVo();
                vo.setUserInfo(userInfo);
                recommendUserVoList.add(vo);
            }
        }
        return ResponseEntity.ok(recommendUserVoList);
    }
    public ResponseEntity love(String cardsId) {
        Long toUserId = UserHolder.getUserId();
        UserLike userLike = new UserLike();
        userLike.setCreated(System.currentTimeMillis());

        userLike.setUserId(toUserId);//自己id
        userLike.setLikeUserId(Long.valueOf(cardsId));//卡片id

        tanHuaService.deleteCards(toUserId,Long.valueOf(cardsId));

        //单方面喜欢
        userLikeCountService.addUserLike(userLike);

        //判断双方是不是互相喜欢
        boolean yesOrNoFriend = userLikeCountService.common(toUserId,Long.valueOf(cardsId));

        if (yesOrNoFriend){
            //环信添加好友
            //huanXinTemplate.addContacts("HX"+toUserId  ,"HX"+cardsId );
            //java添加好友
            friendService.addContacts(toUserId,Long.valueOf(cardsId));
        }
        return ResponseEntity.ok(null);
    }
    public ResponseEntity unlove(String cardsid) {
        Long toUserId = UserHolder.getUserId();
        //根据卡片id查询推荐用户对象
        RecommendUser recommendUser = tanHuaService.findByCarsIdAndLoginId(Long.valueOf(cardsid),toUserId);

        //左滑 以后该用户不会出现在推荐列表
        tanHuaService.deleteCards(toUserId,Long.valueOf(cardsid));
        return ResponseEntity.ok(null);
    }

}
